<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生产过程管理系统 - 登录</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <style>
        :root {
            --primary-color: #18a058;
            --primary-hover: #0e8c46;
            --accent-color: #3498db;
            --dark-color: #2c3e50;
            --light-color: #ecf0f1;
            --danger-color: #e74c3c;
            --success-color: #2ecc71;
            --warning-color: #f39c12;
            --info-color: #3498db;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            height: 100vh;
            overflow: hidden;
            background-color: #f5f5f5;
            position: relative;
        }

        /* 背景图层 */
        .bg-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: url('img/bg.png'); /* 背景图 */
            background-size: cover;
            background-position: center;
            filter: blur(3px);
            z-index: -2;
        }

        /* 背景遮罩 */
        .bg-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0.4) 100%);
            z-index: -1;
        }

        /* 动态粒子背景 */
        .particles {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
        }

        .login-container {
            display: flex;
            width: 100%;
            height: 100vh;
            justify-content: center;
            align-items: center;
            perspective: 1000px;
        }

        .login-card {
            width: 400px;
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            transform-style: preserve-3d;
            transform: rotateY(0deg) translateZ(0);
            transition: all 0.5s ease;
            padding: 0;
        }

        .login-card:hover {
            transform: rotateY(5deg) translateZ(10px);
            box-shadow: -10px 15px 35px rgba(0, 0, 0, 0.3);
        }

        .login-header {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--accent-color) 100%);
            color: white;
            padding: 25px 30px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }

        .login-header h1 {
            font-size: 24px;
            font-weight: 600;
            margin: 0;
            position: relative;
            z-index: 1;
        }

        .login-header p {
            margin: 10px 0 0;
            font-size: 14px;
            opacity: 0.8;
            position: relative;
            z-index: 1;
        }

        /* 头部装饰元素 */
        .header-decoration {
            position: absolute;
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
            top: -100px;
            right: -50px;
        }

        .header-decoration:before {
            content: '';
            position: absolute;
            width: 150px;
            height: 150px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
            top: 50px;
            left: -100px;
        }

        .login-body {
            padding: 30px;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            color: #555;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            height: 50px;
            border-radius: 8px;
            border: 2px solid #e0e0e0;
            padding: 0 15px 0 45px;
            font-size: 16px;
            transition: all 0.3s ease;
            background-color: rgba(255, 255, 255, 0.8);
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(24, 160, 88, 0.2);
            outline: none;
        }

        .input-icon {
            position: absolute;
            left: 15px;
            top: 40px;
            color: #aaa;
            transition: all 0.3s ease;
        }

        .form-control:focus + .input-icon {
            color: var(--primary-color);
        }

        .role-selector {
            display: flex;
            justify-content: space-between;
            margin-bottom: 25px;
        }

        .role-option {
            flex: 1;
            text-align: center;
            padding: 10px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            margin: 0 5px;
        }

        .role-option.active {
            border-color: var(--primary-color);
            background-color: rgba(24, 160, 88, 0.1);
        }

        .role-option i {
            display: block;
            font-size: 24px;
            margin-bottom: 5px;
            color: #555;
        }

        .role-option.active i {
            color: var(--primary-color);
        }

        .role-option span {
            font-size: 14px;
            color: #555;
        }

        .role-option.active span {
            color: var(--primary-color);
            font-weight: 500;
        }

        .btn-login {
            width: 100%;
            height: 50px;
            border-radius: 8px;
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--accent-color) 100%);
            border: none;
            color: white;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .btn-login:hover {
            background: linear-gradient(135deg, var(--primary-hover) 0%, #2980b9 100%);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(24, 160, 88, 0.3);
        }

        .btn-login:active {
            transform: translateY(0);
            box-shadow: none;
        }

        /* 按钮波纹效果 */
        .btn-login:after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 5px;
            height: 5px;
            background: rgba(255, 255, 255, 0.5);
            opacity: 0;
            border-radius: 100%;
            transform: scale(1, 1) translate(-50%);
            transform-origin: 50% 50%;
        }

        @keyframes ripple {
            0% {
                transform: scale(0, 0);
                opacity: 0.5;
            }
            100% {
                transform: scale(20, 20);
                opacity: 0;
            }
        }

        .btn-login:focus:not(:active)::after {
            animation: ripple 1s ease-out;
        }

        .login-footer {
            text-align: center;
            padding: 0 30px 30px;
        }

        .login-footer p {
            font-size: 14px;
            color: #777;
            margin: 0;
        }

        /* 响应式调整 */
        @media (max-width: 480px) {
            .login-card {
                width: 90%;
                max-width: 400px;
            }
            
            .role-selector {
                flex-direction: column;
            }
            
            .role-option {
                margin: 5px 0;
            }
        }

        /* 错误提示 */
        .error-message {
            color: var(--danger-color);
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        /* 加载动画 */
        .spinner {
            display: none;
            width: 20px;
            height: 20px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 0.8s linear infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .btn-login.loading .spinner {
            display: inline-block;
        }

        /* 科技感装饰元素 */
        .tech-decoration {
            position: absolute;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: -1;
        }

        .tech-circle {
            position: absolute;
            border-radius: 50%;
            border: 1px solid rgba(24, 160, 88, 0.2);
            animation: pulse 4s infinite alternate;
        }

        .tech-circle:nth-child(1) {
            width: 300px;
            height: 300px;
            top: -150px;
            left: -150px;
            animation-delay: 0s;
        }

        .tech-circle:nth-child(2) {
            width: 400px;
            height: 400px;
            bottom: -200px;
            right: -200px;
            animation-delay: 1s;
        }

        .tech-circle:nth-child(3) {
            width: 200px;
            height: 200px;
            top: 50%;
            right: -100px;
            animation-delay: 2s;
        }

        @keyframes pulse {
            0% {
                transform: scale(1);
                opacity: 0.2;
            }
            100% {
                transform: scale(1.1);
                opacity: 0.4;
            }
        }
    </style>
</head>
<body>
    <!-- 背景容器 -->
    <div class="bg-container"></div>
    <div class="bg-overlay"></div>
    
    <!-- 粒子背景 -->
    <div id="particles-js" class="particles"></div>
    
    <!-- 科技感装饰元素 -->
    <div class="tech-decoration">
        <div class="tech-circle"></div>
        <div class="tech-circle"></div>
        <div class="tech-circle"></div>
    </div>
    
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <div class="header-decoration"></div>
                <h1>生产过程管理系统</h1>
                <p>Production Process Management System</p>
            </div>
            
            <div class="login-body">
                <form id="loginForm">
                    <!-- 角色选择器 -->
                    <div class="role-selector">
                        <div class="role-option" data-role="普通员工">
                            <i>👤</i>
                            <span>普通员工</span>
                        </div>
                        <div class="role-option" data-role="部门经理">
                            <i>👨‍💼</i>
                            <span>部门经理</span>
                        </div>
                        <div class="role-option" data-role="管理员">
                            <i>🔐</i>
                            <span>系统管理员</span>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="username">工号/手机号</label>
                        <input type="text" class="form-control" id="username" placeholder="请输入工号或手机号">
                        <span class="input-icon">👤</span>
                        <div class="error-message" id="usernameError">请输入有效的工号或手机号</div>
                    </div>
                    
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" placeholder="请输入密码">
                        <span class="input-icon">🔒</span>
                        <div class="error-message" id="passwordError">请输入密码</div>
                    </div>
                    
                    <button type="button" class="btn-login" id="loginBtn">
                        <span class="spinner"></span>
                        <span>登录系统</span>
                    </button>
                </form>
            </div>
            
            <div class="login-footer">
                <p>© 2025 生产过程管理系统 版权所有</p>
            </div>
        </div>
    </div>

    <!-- 引入粒子效果JS -->
    <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
    <script>
        // 检查用户是否已登录
        document.addEventListener('DOMContentLoaded', function() {
            // 如果用户已登录，根据角色跳转到对应页面
            const userInfo = localStorage.getItem('userInfo');
            if (userInfo) {
                try {
                    const user = JSON.parse(userInfo);
                    if (user.token) {
                        // 根据角色跳转到不同的页面
                        const userRole = user.role;
                        if (userRole === '管理员') {
                            window.location.href = 'modules/administrator/admin_index.html';
                        } else if (userRole === '部门经理') {
                            window.location.href = 'modules/manager/manager_index.html';
                        } else if (userRole === '普通员工') {
                            window.location.href = 'modules/employee/employee_index.html';
                        } else {
                            // 默认跳转
                            window.location.href = 'index.html';
                        }
                    }
                } catch (e) {
                    // 解析错误，清除无效的用户信息
                    localStorage.removeItem('userInfo');
                }
            }
        });
    </script>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化粒子背景
            if(typeof particlesJS !== 'undefined') {
                particlesJS("particles-js", {
                    "particles": {
                        "number": {
                            "value": 80,
                            "density": {
                                "enable": true,
                                "value_area": 800
                            }
                        },
                        "color": {
                            "value": "#ffffff"
                        },
                        "shape": {
                            "type": "circle",
                            "stroke": {
                                "width": 0,
                                "color": "#000000"
                            },
                        },
                        "opacity": {
                            "value": 0.5,
                            "random": false,
                            "anim": {
                                "enable": false,
                                "speed": 1,
                                "opacity_min": 0.1,
                                "sync": false
                            }
                        },
                        "size": {
                            "value": 3,
                            "random": true,
                            "anim": {
                                "enable": false,
                                "speed": 40,
                                "size_min": 0.1,
                                "sync": false
                            }
                        },
                        "line_linked": {
                            "enable": true,
                            "distance": 150,
                            "color": "#ffffff",
                            "opacity": 0.4,
                            "width": 1
                        },
                        "move": {
                            "enable": true,
                            "speed": 2,
                            "direction": "none",
                            "random": false,
                            "straight": false,
                            "out_mode": "out",
                            "bounce": false,
                        }
                    },
                    "interactivity": {
                        "detect_on": "canvas",
                        "events": {
                            "onhover": {
                                "enable": true,
                                "mode": "grab"
                            },
                            "onclick": {
                                "enable": true,
                                "mode": "push"
                            },
                            "resize": true
                        },
                        "modes": {
                            "grab": {
                                "distance": 140,
                                "line_linked": {
                                    "opacity": 1
                                }
                            },
                            "push": {
                                "particles_nb": 4
                            }
                        }
                    },
                    "retina_detect": true
                });
            }

            // 角色选择
            const roleOptions = document.querySelectorAll('.role-option');
            let selectedRole = null;

            roleOptions.forEach(option => {
                option.addEventListener('click', function() {
                    roleOptions.forEach(opt => opt.classList.remove('active'));
                    this.classList.add('active');
                    selectedRole = this.getAttribute('data-role');
                });
            });

            // 默认选中普通员工
            document.querySelector('[data-role="普通员工"]').classList.add('active');
            selectedRole = "普通员工";

            // 登录按钮点击事件
            const loginBtn = document.getElementById('loginBtn');
            const loginForm = document.getElementById('loginForm');
            const usernameInput = document.getElementById('username');
            const passwordInput = document.getElementById('password');
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');

            loginBtn.addEventListener('click', function() {
                // 重置错误信息
                usernameError.style.display = 'none';
                passwordError.style.display = 'none';

                // 表单验证
                let isValid = true;

                if (!usernameInput.value.trim()) {
                    usernameError.style.display = 'block';
                    isValid = false;
                }

                if (!passwordInput.value.trim()) {
                    passwordError.style.display = 'block';
                    isValid = false;
                }

                if (!selectedRole) {
                    alert('请选择登录角色');
                    isValid = false;
                }

                if (isValid) {
                    // 显示加载状态
                    loginBtn.classList.add('loading');
                    loginBtn.disabled = true;

                    // 准备登录数据
                    const loginData = {
                        username: usernameInput.value.trim(),
                        password: passwordInput.value.trim(),
                        role: selectedRole
                    };

                    // 发送登录请求
                    fetch('http://localhost:8080/api/auth/login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(loginData)
                    })
                    .then(response => {
                        if (!response.ok) {
                            return response.json().then(data => {
                                throw new Error(data.message || '登录失败，请检查用户名和密码');
                            });
                        }
                        return response.json();
                    })
                    .then(data => {
                        // 校验返回的角色是否与用户选择的角色一致
                        if (data.role !== selectedRole) {
                            throw new Error('账号与登录身份不匹配');
                        }

                        // 登录成功，保存用户信息到本地存储
                        localStorage.setItem('userInfo', JSON.stringify(data));

                        // 根据角色跳转到不同的页面
                        const userRole = data.role;
                        if (userRole === '管理员') {
                            window.location.href = 'modules/administrator/admin_index.html';
                        } else if (userRole === '部门经理') {
                            window.location.href = 'modules/manager/manager_index.html';
                        } else if (userRole === '普通员工') {
                            window.location.href = 'modules/employee/employee_index.html';
                        } else {
                            // 默认跳转
                            window.location.href = 'index.html';
                        }
                    })
                    .catch(error => {
                        // 显示错误信息
                        alert(error.message);
                    })
                    .finally(() => {
                        // 隐藏加载状态
                        loginBtn.classList.remove('loading');
                        loginBtn.disabled = false;
                    });
                }
            });
        });
    </script>
</body>
</html>